import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import pearsonr

def correlation_distance(x, y):
    corr, _ = pearsonr(x, y)
    return 1 - abs(corr)

# 创建数据
np.random.seed(0)
x = np.linspace(0, 10, 100)
y1 = x + np.random.normal(0, 1, 100)  # 正相关
y2 = -x + np.random.normal(0, 1, 100)  # 负相关
y3 = np.random.normal(0, 1, 100)  # 无相关

# 计算相关距离
d1 = correlation_distance(x, y1)
d2 = correlation_distance(x, y2)
d3 = correlation_distance(x, y3)

# 绘制散点图
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(18, 6))

ax1.scatter(x, y1)
ax1.set_title(f'Positive Correlation\nDistance = {d1:.2f}')
ax1.set_xlabel('X')
ax1.set_ylabel('Y')

ax2.scatter(x, y2)
ax2.set_title(f'Negative Correlation\nDistance = {d2:.2f}')
ax2.set_xlabel('X')
ax2.set_ylabel('Y')

ax3.scatter(x, y3)
ax3.set_title(f'No Correlation\nDistance = {d3:.2f}')
ax3.set_xlabel('X')
ax3.set_ylabel('Y')

plt.tight_layout()
plt.show()
